<template>
  <div class="sync-strategy-list">
    <el-card>
      <div slot="header" class="card-header">
        <span>同步策略管理</span>
        <div class="header-actions">
          <el-button type="primary" @click="handleAdd">
            <i class="el-icon-plus"></i> 添加策略
          </el-button>
          <el-button @click="handleRefresh">
            <i class="el-icon-refresh"></i> 刷新
          </el-button>
        </div>
      </div>
      
      <el-table :data="strategyList" style="width: 100%" empty-text="暂无同步策略数据，请先添加">
        <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
        <el-table-column prop="strategyName" label="策略名称" min-width="180"></el-table-column>
        <el-table-column prop="sourceDbConfigId" label="源数据库ID" width="120" align="center"></el-table-column>
        <el-table-column prop="targetDbConfigId" label="目标数据库ID" width="120" align="center"></el-table-column>
        <el-table-column prop="syncType" label="同步类型" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.syncType === 'FULL'" type="success">全量</el-tag>
            <el-tag v-else type="info">增量</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="syncInterval" label="同步间隔(秒)" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.syncInterval === 0 ? '手动' : scope.row.syncInterval }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 'ENABLED'" type="success">启用</el-tag>
            <el-tag v-else type="warning">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="80" fixed="right" align="center">
          <template slot-scope="scope">
            <el-dropdown size="small">
              <el-button size="small" type="text">
                <i class="el-icon-setting"></i> 操作<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleEdit(scope.row)">
                  <i class="el-icon-edit"></i> 编辑
                </el-dropdown-item>
                <el-dropdown-item divided @click.native="handleDelete(scope.row)">
                  <i class="el-icon-delete"></i> 删除
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          :current-page="currentPage"
          :page-size="pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'SyncStrategyList',
  data() {
    return {
      strategyList: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      loading: false
    };
  },
  mounted() {
    this.loadStrategies();
  },
  methods: {
    loadStrategies() {
      this.loading = true;
      this.$api.syncStrategyApi.getAll()
        .then(data => {
          // http.js已经处理了响应格式，这里直接获取数据
          const strategyData = data && data.data || [];
          this.strategyList = strategyData;
          this.total = strategyData.length;
          this.loading = false;
        })
        .catch(error => {
          console.error('获取同步策略列表失败:', error);
          this.$message.error('获取同步策略列表失败');
          this.loading = false;
        });
    },
    
    handleAdd() {
      this.$router.push('/sync-strategy/add');
    },
    
    handleEdit(row) {
      this.$router.push({
        path: '/sync-strategy/edit',
        query: { id: row.id }
      });
    },
    
    handleDelete(row) {
      this.$confirm('确定要删除该同步策略吗？删除后无法恢复。', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.syncStrategyApi.delete(row.id)
            .then(() => {
              this.$message.success('删除成功');
              this.loadStrategies();
            })
          .catch(error => {
            this.$message.error('删除失败: ' + error.message);
          });
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    
    handleSizeChange(size) {
      this.pageSize = size;
      this.loadStrategies();
    },
    
    handleCurrentChange(current) {
      this.currentPage = current;
      this.loadStrategies();
    },
    
    // 刷新策略列表
    handleRefresh() {
      this.loadStrategies();
    }
  }
};
</script>

<style scoped>
.sync-strategy-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
}

.header-actions {
  display: flex;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>